//package com.ant.storage.config;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import org.apache.http.HttpResponse;
//import org.apache.http.HttpStatus;
//import org.apache.http.client.HttpClient;
//import org.apache.http.client.methods.HttpGet;
//import org.apache.http.impl.client.HttpClients;
//import org.apache.http.util.EntityUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.web.servlet.HandlerInterceptor;
//
//import javax.servlet.ServletContext;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.PrintWriter;
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.HashMap;
//import java.util.Map;
//
//
///**
// * @author hzd
// * @version 1.0
// * @date 2022/3/16 9:15
// */
//public class TokenInterceptor implements HandlerInterceptor {
//
//    @Value("${loginUrl.url}")
//    private String Url;
//
//    @Autowired
//    public ServletContext servletContext;
//
//    private final static String URL_WHITELIST[] = {
//            "/hbApi/login/*",
//            "/swagger-ui.html",
//            "/swagger-resources/**",
//            "/storageApi/doc.html",
//            "/swagger/**",
//            "/captcha",
//            "/hbApi/image/**"};
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        String token = request.getHeader("token");
//        String url = request.getRequestURI();
//        if ("OPTIONS".equals(request.getMethod())) {
//            return true;
//        }
//        if (url.equals("/storageApi/DingDing/CallBack")) {
//            return true;
//        }
//        if (new ArrayList<>(Arrays.asList(URL_WHITELIST)).contains(url) || url.contains(".jpg") || url.contains(".png")) {
//            return true;
//        }
//
//
//        HttpClient client = HttpClients.createDefault();
//        //登录接口路径
//        String urls = Url+"/userSystem/user/findUserTokenByToken";
//        HttpGet get = new HttpGet(urls);
//        JSONObject jsonObject = null;
//        try {
//            get.addHeader("content-type", "application/json");
//            get.addHeader("Token", token);
//            HttpResponse res = client.execute(get);
//            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//                String str = EntityUtils.toString(res.getEntity());
//                jsonObject = JSONObject.parseObject(str);
//                String ss = jsonObject.get("code").toString();
//              //  UserInfo one = userInfoService.getOne(new QueryWrapper<UserInfo>().eq("token", token));
////                if (ss.equals("401") || one==null) {
////                    returnJson(response, "登录超时");
////                    return false;
////                } else{
////                    servletContext.setAttribute("userInfo",one.getUserMsg());
////                }
//            }
//        } catch (Exception e) {
//            returnJson(response, "token已经过期");
//            e.printStackTrace();
//        }
//        return true;
//    }
//
//    /**
//     * 返回格式化后的json到前端页面
//     *
//     * @param response
//     * @param message
//     * @throws Exception
//     */
//    private void returnJson(HttpServletResponse response, String message) throws Exception {
//        response.setContentType("application/json;charset=UTF-8");
//        PrintWriter writer =response.getWriter();
//        // 注意点1：这边返回配置为josn格式
//        try {
//            Map<String, Object> result = new HashMap<>();
//            result.put("code", 403);
//            result.put("message", message);
//            // 注意点2，这样要用fastjson转换一下后，返回的前端才是格式化后的json格式
//            writer.print(JSON.toJSONString(result));
//            writer.close();
//            writer.flush();
//        } finally {
//            if (writer != null) {
//                writer.close();
//                writer.flush();
//            }
//        }
//    }
//
//}
